<template>
  <div class="content">
    <div class="top-search">
      <date-picker :date="initDate" @query="query" />
    </div>
    <div class="item-wrapper">
      <van-pull-refresh v-model="refreshing" class="list" @refresh="onRefresh">
        <van-list
          v-model:loading="loading"
          :finished="finished"
          finished-text="没有更多了"
          @load="onLoad"
        >
          <income-item :income-data="resultData" />
        </van-list>
      </van-pull-refresh>
    </div>

  </div>
</template>
<script setup lang="ts">
import { onMounted, ref } from 'vue';
import datePicker from '../date-picker/date-picker.vue';
import { Toast } from 'vant';
import incomeItem from './income-item.vue';
import moment from 'moment';
import { appApi } from '@/api';
import { LoginUser } from '@/interface/home';
import { IIncome, IParam } from '@/interface/income';

const props = defineProps({
  loginUser: {
    type: Object as () => LoginUser,
    default: () => ({}),
  }
})


// 下拉刷新标识
const refreshing = ref(false);
// 列表加载标识
const loading = ref(false);
// 列表加载完成标识
const finished = ref(false);
// 初始日期
const initDate = ref(moment().format('YYYYMM'));
// 查询参数
const queryParam = ref<IParam>({
  ny: initDate.value,
  orgcode: '',
  sortType: 0,
});
// 查询数据
const resultData = ref<IIncome>({
  byJie: 0,
  byDai: 0,
  qcye: 0,
  byye: 0,
  list: []
});

onMounted(() => {
  console.log('props.loginUser',props.loginUser);
  queryParam.value.orgcode = props.loginUser.orgCode;
  //query(queryParam.value.ny);
})

const query = async (date: string) => {
  try {
    queryParam.value.ny = date;
    await findSzOpen();
    Toast('查询成功');
  } catch (error) {
    console.log('查询收支公开数据失败',error);
  }
}

const findSzOpen = async () => {
  await appApi.findSzOpen(queryParam.value.orgcode,queryParam.value.ny,'0').then((res :any) => {
    resultData.value = res;
  }).catch((err) => {
    Toast('查询收支公开数据失败：' + err);
  })
}

const onRefresh = () => {
  // 清空列表数据
  finished.value = false;
  // 重新加载数据
  // 将 loading 设置为 true，表示处于加载状态
  loading.value = true;
  onLoad();
}

const onLoad = () => {
  setTimeout(async () => {
    if (refreshing.value) {
      refreshing.value = false;
    }
    try {
      await findSzOpen();
      Toast('刷新成功');
    } catch (error) {
      console.log('刷新失败',error);
    }
    loading.value = false;
    finished.value = true;
  }, 200);
};

</script>

<style lang="less" scoped>
.content {
  display: flex;
  flex-direction: column;
  height: 100%;
  .top-search {
    display: flex;
    height: fit-content;
  }
  .item-wrapper {
    padding: 0;
    flex: 1;
    min-height: 0;
    display: flex;
    flex-direction: column;
  }
  .list {
    flex: 1;
    overflow: auto;
    -webkit-overflow-scrolling: touch;
  }
}
</style>
